package cn.ven.controller;

import cn.ven.entity.*;
import cn.ven.service.*;


import cn.ven.tool.RespJson;
import cn.ven.tool.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/*
fore为用户操作，主要有以下功能
1./user/**    修改用户信息，修改自己密码
2./blog/**    添加博客，假删除博客，真删除博客，查看假删除博客，恢复假删除博客
3./like/**    点赞博客，取消点赞博客
4./focus/**   用户关注用户，用户取消用户关注，查看自己关注的所有用户/粉丝
5./collect/**  收藏博客，取消收藏博客
6./comment/**   评论博客，删除评论
 */
@Controller
@CrossOrigin
@RequestMapping("/api/fore")
public class ForeController {
    private Logger logger =  LoggerFactory.getLogger(ForeController.class);

    @Autowired
    UserService userService;

    @Autowired
    BlogService blogService;

    @Autowired
    ContentService contentService;

    @Autowired
    LargeFileService largeFileService;

    @Autowired
    LikeService likeService;

    @Autowired
    FocusService focusService;

    @Autowired
    CollectService collectService;

    @Autowired
    CommentService commentService;

    //用户修改自己的信息
    @RequestMapping("/user/updateUser")
    @ResponseBody
    public Map<String,Object> updateUser( MyUser user,
                                          @RequestParam("pic")MultipartFile file) throws Exception{
        Largefile largefile =new Largefile();
        if (file!=null&&file.getSize()>0){//判断是否已经有了图片，如果有就更新
            largefile.setFilename(file.getOriginalFilename());//获取文件原名
            logger.info("originFilename====="+file.getOriginalFilename());
            byte[] bytes = file.getBytes();
            largefile.setContent(bytes);
            String newName =UUID.randomUUID().toString();//获取唯一名
            largefile.setId(newName);
            largeFileService.addLargefile(largefile);
        }
        user.setPicture(largefile.getId());
        userService.updateUser(user);
        return RespJson.pack(200,"修改成功",user);
    }


    //添加博客
    //tagid在添加已经直接添加了
    //前端走键值对
    @RequestMapping("/blog/addBlog")
    @ResponseBody
    public Map<String,Object> addBlog(Blog blog,Content content){
        contentService.addContent(content);
        logger.info("contentid==="+content.getId());
        blog.setContentid(content.getId());
        blogService.addBlog(blog);
        return RespJson.pack(200,"添加成功",blog);
    }

    //假删除博客
    @RequestMapping("/blog/removeBlog/{id}")
    @ResponseBody
    public Map<String,Object> removeBlog(@PathVariable("id")Integer id){
        Blog blog = blogService.findById(id);
        blog.setClosed(0);//假删除
        blogService.updateBlog(blog);
        List<UserTagBlogContent> userTagBlogContents = blogService.findBlogByClosed(1);//把没假删除的显示出来
        return RespJson.pack(200,"删除成功",userTagBlogContents);
    }

    //恢复假删除博客
    @RequestMapping("/blog/recoverBlog/{id}")
    @ResponseBody
    public Map<String,Object> recovereBlog(@PathVariable("id")Integer id){
        Blog blog = blogService.findById(id);
        blog.setClosed(1);//恢复假删除
        blogService.updateBlog(blog);
        List<UserTagBlogContent> userTagBlogContents = blogService.findBlogByClosed(0);
        return RespJson.pack(200,"恢复成功",userTagBlogContents);
    }

//    //真删除
//    @RequestMapping("/blog/delBlog/{id}")
//    @ResponseBody
//    public Map<String,Object> delBlog(@PathVariable("id")Integer id){
//        blogService.delBlog(id);
//        contentService.delContent(id);
//        List<UserTagBlogContent> userTagBlogContents = blogService.findBlogByClosed(0);
//        return RespJson.pack(200,"彻底删除成功",userTagBlogContents);
//    }

    //查看假删除的博客
    @RequestMapping("/blog/findMyRemoveBlogs")
    @ResponseBody
    public Map<String,Object> findMyRemoveBlogs(){
        List<UserTagBlogContent> userTagBlogContents = blogService.findBlogByClosed(1);
        return RespJson.pack(200,"",userTagBlogContents);
    }

    //点赞
    @RequestMapping("/like/addLike/{blogid}")
    @ResponseBody
    public Map<String,Object> addLike(@PathVariable("blogid")Integer blogid,Like like){
        like.setBlogid(blogid);
        if (likeService.addLike(like)==1){  //判断是否点过赞了，点赞表是否
            return RespJson.pack(200,"点赞成功","");
        }
        return RespJson.pack(500,"你已经点过赞了","");
    }


    //取消点赞
    @RequestMapping("/like/cancelLike/{blogid}")
    @ResponseBody
    public Map<String,Object> cancelLike(@PathVariable("blogid")Integer blogid){
        if(likeService.delLike(blogid)==1){
            return RespJson.pack(200,"取消点赞成功","");
        }
        return RespJson.pack(500,"操作失败","");
    }


    //关注用户
    @RequestMapping("/focus/addFocus")
    @ResponseBody
    public Map<String,Object> addFocus(@RequestBody Focus focus){
        System.out.println("focus==="+focus);
        int i = focusService.addFocus(focus);
        if (i==1){
            return RespJson.pack(200,"关注成功",focus);
        }
        return  RespJson.pack(500,"关注失败,","");
    }

    //取消关注用户
    @RequestMapping("/focus/cancelFocus/{focuseduid}")
    @ResponseBody
    public Map<String,Object> cancelFocus(@PathVariable("focuseduid")Integer focuseduid){
        int i = focusService.cancelFocus(focuseduid);
        if (i==1){
            return RespJson.pack(200,"取消关注成功","");
        }
        return RespJson.pack(500,"取消关注失败","");
    }

    //查看自己关注的用户
    @RequestMapping("/focus/findMyFocusedUser/{focuseduid}")
    @ResponseBody
    public Map<String,Object> findMyFocusedUser(@PathVariable("focuseduid")Integer focuseduid){
        List<FocusUser> allFocusedUser = focusService.findAllFocusedUser(focuseduid);
        return RespJson.pack(200,"",allFocusedUser);
    }

    //查看自己的粉丝
    @RequestMapping("/focus/findMyFocusUser")
    @ResponseBody
    public Map<String,Object> findMyFocusUser(){
        List<FocusUser> allFocusUser = focusService.findAllFocusUser();
        return RespJson.pack(200,"",allFocusUser);
    }



    //收藏博客
    @RequestMapping("/collect/collectBlog")
    @ResponseBody
    public Map<String,Object> collectBlog(@RequestBody Collect collect){
        int i = collectService.addCollect(collect);
        if (i==1){
            return RespJson.pack(200,"收藏成功",collect);
        }
        return RespJson.pack(500,"收藏失败，你已经收藏过了","");
    }

    //取消收藏
    @RequestMapping("/collect/cancelCollect/{blogid}")
    @ResponseBody
    public Map<String,Object> cancelCollect(@PathVariable("blogid")Integer blogid){
        int i = collectService.cancelCollect(blogid);
        if(i==1){
            return RespJson.pack(200,"取消收藏","");
        }
        return RespJson.pack(500,"取消失败","");
    }


    //添加评论
    @RequestMapping("/comment/addComment")
    @ResponseBody
    public Map<String,Object> addComment(@RequestBody Comment comment){
        commentService.addComment(comment);
        return RespJson.pack(200,"评论成功",comment);
    }

    //删除评论
    @RequestMapping("/comment/delComment/{id}")
    @ResponseBody
    public Map<String,Object> delComment(@PathVariable("id")Integer id){
        commentService.delComment(id);
        return RespJson.pack(200,"删除成功","");

    }

    @RequestMapping("/getUser")
    @ResponseBody
    public Object getUser(){
        Object principal =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println("principal="+principal);
        if(principal ==null) return null;
        if(principal instanceof UserDetails){
            return (UserDetails) principal;
        }else {
            return String.valueOf(principal);
        }
    }




    


}
